﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DblogStudio.BLL;
using DblogStudio.DAL.Models;
using DblogStudio.GUI.Common;
using DblogStudio.GUI.Utils;

namespace DblogStudio.GUI.DuLieu
{
    public partial class FormSaoChepChiTietMay : DblogStudio.GUI.Common.BaseDialog
    {
        MaySanXuat maySanXuat;
        public FormSaoChepChiTietMay(MaySanXuat maySanXuat)
        {
            InitializeComponent();
            lbMay.Text = string.Format(lbMay.Text, maySanXuat.TenMaySanXuat);
            this.maySanXuat = maySanXuat;
            InitTree();
        }

        private void InitTree()
        {
            #region Khởi tạo cây
            treeChiTietMay.Nodes.Clear();
            var boldFont = new System.Drawing.Font(treeChiTietMay.Font, FontStyle.Bold);
            var lsMay = BizMaySanXuat.GetList();
            if (lsMay != null && lsMay.Count > 0)
            {
                foreach (var may in lsMay)
                {
                    if (may.MaMaySanXuat != maySanXuat.MaMaySanXuat)
                    {
                        var nodeMay = new TreeNode(may.TenMaySanXuat);
                        nodeMay.Tag = may.MaMaySanXuat;
                        nodeMay.NodeFont = boldFont; ;
                        treeChiTietMay.Nodes.Add(nodeMay);
                    }
                }
            }
            #endregion
        }
        CEMS4UG_DHKHContext _db = null;
        public CEMS4UG_DHKHContext db
        {
            get
            {
                if (_db == null)
                    _db = CEMS4UG_DHKHContext.GetContext();
                return _db;
            }
        }

        private void btnSaoChep_Click(object sender, EventArgs e)
        {
            int count = 0;
            var fp = new FormProgress();
            fp.DoWork += (o, t) =>
            {
                foreach (TreeNode node in treeChiTietMay.Nodes)
                {
                    if (node.Checked)
                    {
                        count++;
                        BizMaySanXuat.SaoChep(maySanXuat.MaMaySanXuat, node.Tag.ToString(), db);
                    }
                }
            };
            fp.RunWorkerCompleted += (o, t) =>
            {
                DialogUtils.ShowMessage("Đã sao chép tới " + count + " máy thành công");
                DialogResult = System.Windows.Forms.DialogResult.OK;
            };
            fp.StartWorking(this);
        }

    }
}
